import 'package:flutter/material.dart';

//通过外部传入的布局行为，来进行布局的控件
class CustomSingleChildLayoutWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: new AppBar(
          title: new Text("CustomSingleChildLayout"),
          leading: new BackButton(),
        ),
        body: new Container(
          color: Colors.blue,
          padding: const EdgeInsets.all(5),
          child: CustomSingleChildLayout(
            delegate: FixedSizeLayoutDelegate(Size(200, 200)),
            child: Container(
              color: Colors.red,
              width: 100,
              height: 300,
            ),
          ),
        ));
  }
}

/*  CustomSingleChildLayout提供了一个控制child布局的delegate，这个delegate可以控制这些因素：
    可以控制child的布局constraints；
    可以控制child的位置；
    在parent的尺寸不依赖于child的情况下，可以决定parent的尺寸。*/
class FixedSizeLayoutDelegate extends SingleChildLayoutDelegate {
  FixedSizeLayoutDelegate(this.size);

  final Size size;

  @override
  Size getSize(BoxConstraints constraints) => size;

  @override
  BoxConstraints getConstraintsForChild(BoxConstraints constraints) =>
      new BoxConstraints.tight(size);

  //判断是否需要重新布局的
  @override
  bool shouldRelayout(FixedSizeLayoutDelegate oldDelegate) =>
      size != oldDelegate.size;
}
